﻿using System;
using System.Data;
using System.Windows.Forms;
using NDK.Module.DAL;
using NDK.Module.Model;
using System.Data.SQLite;
using System.Threading;
using FingerPrintCollection;
using BZ.Core.TaskExt;
using BZ.Logger;

namespace NDK.Frm
{
    public partial class Dlg_AdminFPRegiste : Form
    {

        UserFPInfoServices userFPInfoServices = new UserFPInfoServices();
        DataTable userInfo = new DataTable();
        SQLiteDataAdapter userDataAdapter = new SQLiteDataAdapter();
        Thread startRegisterFP = null;
        TipForm tipForm = new TipForm();

        public Dlg_AdminFPRegiste()
        {
            InitializeComponent();
        }

        private void StartBtn_Click(object sender, EventArgs e)
        {
            Common.FPUserInfo.UserName = "Admin";

            if (AdminNameTxt.Text != "")
            {
                FPFuction.FPInfo.UserName = AdminNameTxt.Text.Trim();

                //查询数据库是否存在该工号的指纹
                int Count_ID = userFPInfoServices.InquirySpecifyInfo2Table("UserName", FPFuction.FPInfo.UserName, "AdminFPInfo");

                if (Count_ID > 3) //最多允许有3次记录
                {
                    MessageBox.Show("用户名 " + FPFuction.FPInfo.UserName + "已被注册过,请勿重新注册!!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Common.AppendUiLog.Log("用户名已被注册过,请勿重新录入!!");
                    return;
                }

            }
            else
            {
                MessageBox.Show("用户名不能为空!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            FPFuction.FPInfo.UserAuthority = "Admin";

            FPFuction.b_startScanFP = false;

            startRegisterFP = TaskManager.Run(()=>
            {
                StartFP();
            });
            //startRegisterFP = new Thread(new ThreadStart(StartFP));
            //startRegisterFP.IsBackground = true;
            //startRegisterFP.Start();

            tipForm = new TipForm();
            tipForm.Show(this);
        }

        /// <summary>
        /// 指纹录入函数
        /// </summary>
        private void StartFP()
        {
            if (!FPFuction.IsRegister)
            {
                FPFuction.IsRegister = true;
                FPFuction.RegisterCount = 0;
                FPFuction.cbRegTmp = 0;
                FPFuction.AppendUiLog.Log("开始录入指纹,请相同手指按压指纹3次...", LogType.Info);
            }
        }
    }
}
